<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Order;

use Srv\Apps\Apis\Order\Ctl\OrderCtl;
use Srv\Apps\Proto\DTO\Order\PayOrderCodeDTO;
use Srv\Apps\Proto\DTO\Order\PayOrderListDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\VO\Order\PayOrderListVO;
use Srv\Apps\Proto\VO\Order\PayThreeOrderDataVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class OrderAct extends ActionAbstract
{
    /**
     * @url /pay/order/list
     * @auth payOrderList
     * @req Srv.Apps.Proto.DTO.Order.PayOrderListDTO
     * @rsp Srv.Apps.Proto.VO.Order.PayOrderListVO
     * 支付产品订单列表
     */
    public function getPayOrderList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof PayOrderListDTO) || !($VOClass instanceof PayOrderListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = OrderCtl::tag()->getPayOrderList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /pay/three/order/data/code
     * @auth getPayThreeOrderByCode
     * @req Srv.Apps.Proto.DTO.Order.PayOrderCodeDTO
     * @rsp Srv.Apps.Proto.VO.Order.PayThreeOrderDataVO
     * 支付第三方订单数据
     */
    public function getPayThreeOrderByCode(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($RouteConfData);
        if(!($DTOClass instanceof PayOrderCodeDTO) || !($VOClass instanceof PayThreeOrderDataVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $OrderCtl               = OrderCtl::tag();
        $orderCode              = $DTOClass->getCode();
        $orderSrc               = $OrderCtl->getPayOrderSrcByCode($orderCode);
        if(!isset($orderSrc['porr_id'])) {
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        }
        $payType                = intval($orderSrc['porr_pay_type']);
        $VOClass                = $OrderCtl->getPayThreeOrderDataByCode($orderCode, $payType);
        return ResponseData::rspSuccAuto('', $VOClass);
    }
}